package org.jeecg.modules.xl.kuaimai.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.xl.kuaimai.KuaimaiAPIConfig;
import org.jeecg.modules.xl.kuaimai.entity.XlKuaimaiStock;
import org.jeecg.modules.xl.kuaimai.mapper.XlKuaimaiStockMapper;
import org.jeecg.modules.xl.kuaimai.vo.KMStockStatusRespVO;
import org.quartz.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 同步定时任务测试
 *
 * 此处的同步是指 当定时任务的执行时间大于任务的时间间隔时
 * 会等待第一个任务执行完成才会走第二个任务
 *
 *
 * @author: taoyan
 * @date: 2020年06月19日
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class KMAsyncJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(" --- 同步任务调度开始 --- ");
        try {
            //https://open.kuaimai.com/docs/api/API%E6%96%87%E6%A1%A3/%E5%95%86%E5%93%81/%E6%9F%A5%E8%AF%A2%E5%BA%93%E5%AD%98%E7%8A%B6%E6%80%81/
            Map<String, String> firstPagePara = KuaimaiAPIConfig.getKMStockParam("100","1");

            String firstPageStr = HttpUtil.post("https://gw3.superboss.cc/router", JSON.toJSONString(firstPagePara));

            KMStockStatusRespVO FirstPagekmStockStatusResp = JSON.parseObject(firstPageStr, KMStockStatusRespVO.class);

            //System.out.println(firstPageStr);
            int total = FirstPagekmStockStatusResp.getTotal().intValue();//数据总量

            //计算总页数
            int pageTotal =  (int)Math.ceil( (double)total / 100 );

            List<XlKuaimaiStock> haotemaiStockList = new ArrayList<>();
            //处理第一页
            //System.out.println(FirstPagekmStockStatusResp.getMsg());
            //System.out.println(FirstPagekmStockStatusResp.getTotal());
            //System.out.println(FirstPagekmStockStatusResp.getStockStatusVoList().size());
            List<XlKuaimaiStock> firstPageList = FirstPagekmStockStatusResp.getStockStatusVoList();
            Set<String> uniqueIdSet = new HashSet<>();
            for(XlKuaimaiStock  xlKuaimaiStockTemp : firstPageList){
                if(CategoryValidator.isFangziCategory(xlKuaimaiStockTemp.getCidname())){
                    //如果含有纺织类，不处理
                    continue;
                }
                if(uniqueIdSet.contains(xlKuaimaiStockTemp.getMainouterid())){
                    continue;
                }
                uniqueIdSet.add(xlKuaimaiStockTemp.getMainouterid());
                xlKuaimaiStockTemp.setSltAvailableStock(xlKuaimaiStockTemp.getTotalavailablestock());
                xlKuaimaiStockTemp.setSltLockStock(0L);
                haotemaiStockList.add(xlKuaimaiStockTemp);
            }

            Thread.sleep(1000);
            //处理第二页以后
            for(int i = 2;i <= pageTotal; i++){
                log.info("处理第{}页的数据", i);
                Map<String, String> paramsTep = KuaimaiAPIConfig.getKMStockParam("100",String.valueOf(i));
                String resultJsonTemp = HttpUtil.post("https://gw3.superboss.cc/router", JSON.toJSONString(paramsTep));

                KMStockStatusRespVO kmStockStatusRespTemp = JSON.parseObject(resultJsonTemp, KMStockStatusRespVO.class);

                for(XlKuaimaiStock  xlKuaimaiStockTemp : kmStockStatusRespTemp.getStockStatusVoList()){
                    if(CategoryValidator.isFangziCategory(xlKuaimaiStockTemp.getCidname())){
                        //如果含有纺织类，不处理
                        continue;
                    }
                    xlKuaimaiStockTemp.getBrand();
                    if(uniqueIdSet.contains(xlKuaimaiStockTemp.getMainouterid())){
                        continue;
                    }
                    uniqueIdSet.add(xlKuaimaiStockTemp.getMainouterid());
                    xlKuaimaiStockTemp.setSltAvailableStock(xlKuaimaiStockTemp.getTotalavailablestock());
                    xlKuaimaiStockTemp.setSltLockStock(0L);
                    haotemaiStockList.add(xlKuaimaiStockTemp);
                }
            }


            XlKuaimaiStockMapper xlKuaimaiStockMapper =  SpringContextUtils.getBean(XlKuaimaiStockMapper.class);
            //先清空表
            xlKuaimaiStockMapper.truncate();
            //在批量插入
            xlKuaimaiStockMapper.batchInsert(haotemaiStockList);

            log.info("广州好特麦的库存流水记录有{}条",haotemaiStockList.size());

        } catch (Exception e) {
            e.printStackTrace();
        }
        //测试发现 每5秒执行一次
        log.info(" --- 执行完毕，时间："+DateUtils.now()+"---");
    }

}
 class CategoryValidator {

    // 使用 HashSet，查询性能 O(1)
    private static final Set<String> CATEGORY_SET = new HashSet<>();

    static {
        CATEGORY_SET.add("四季被");
        CATEGORY_SET.add("冬被");
        CATEGORY_SET.add("服装");
        CATEGORY_SET.add("被子/被芯");
        CATEGORY_SET.add("枕芯");
        CATEGORY_SET.add("床上纺织用品");
        CATEGORY_SET.add("床品套件");
        CATEGORY_SET.add("记忆棉枕芯");
        CATEGORY_SET.add("套件");
        CATEGORY_SET.add("蚊帐");
        CATEGORY_SET.add("枕巾枕套");
        CATEGORY_SET.add("三件套");
        CATEGORY_SET.add("普通床垫");
        CATEGORY_SET.add("功能性枕");
        CATEGORY_SET.add("四件套");
        CATEGORY_SET.add("夏被");
        CATEGORY_SET.add("床单");
        CATEGORY_SET.add("单被套");
        CATEGORY_SET.add("普通毛毯");
        CATEGORY_SET.add("被子");
        CATEGORY_SET.add("羽丝枕芯");
        CATEGORY_SET.add("枕芯/枕头");
        CATEGORY_SET.add("枕头");
        CATEGORY_SET.add("枕巾");
        CATEGORY_SET.add("床笠");
        CATEGORY_SET.add("毛衫");
        CATEGORY_SET.add("凉感软席");
        CATEGORY_SET.add("衣物");
        CATEGORY_SET.add("纺织用品");
        CATEGORY_SET.add("乳胶凉席");
        CATEGORY_SET.add("床头罩");
        CATEGORY_SET.add("套件枕芯");
        CATEGORY_SET.add("电热毯");
    }

    public static boolean isFangziCategory(String cidname) {
        return cidname != null && CATEGORY_SET.contains(cidname.trim());
    }
}
